//
//  DetailViewController.m
//  BookRank
//
//  Created by wststar on 14-2-28.
//  Copyright (c) 2014年 wststar. All rights reserved.
//

#import "DetailViewController.h"
#import "WeiboModel.h"
#import "WeiboView.h"
#import "CommentModel.h"
#import "CommentTableView.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self _initView];
    
    [self loadData];
    
}

-(void)_initView{
    UIView * tableHeadView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)]autorelease];
    tableHeadView.backgroundColor = [UIColor clearColor];
    
    NSString * userImageUrl = self.weiboModel.user.profile_image_url;
    self.userImageView.layer.cornerRadius = 5;
    self.userImageView.layer.masksToBounds = YES;
    [self.userImageView setImageWithURL:[NSURL URLWithString:userImageUrl]];
    
    self.nickLabel.text = self.weiboModel.user.screen_name;
    [tableHeadView addSubview:self.userBarView];
    tableHeadView.height += 60;
      
    
    //创建微博视图=============
    float h = [WeiboView getWeiboViewHeight:self.weiboModel isRepost:NO isDetail:YES];
    _weiboView = [[[WeiboView alloc]initWithFrame:CGRectMake(10, self.userBarView.bottom+10, ScreenWidth-20, h)]autorelease];
    _weiboView.isDetail = YES;
    _weiboView.weiboModel = self.weiboModel;
    [tableHeadView addSubview:_weiboView];
    tableHeadView.height += (h+10);
    
    self.tableView.tableHeaderView = tableHeadView;
    
}

-(void)loadData{
    NSString * weiboId = [_weiboModel.weiboId stringValue];
    if (weiboId.length == 0) {
        return ;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObject:weiboId forKey:@"id"];
    [self.sinaweibo requestWithURL:@"comments/show.json" params:params httpMethod:@"GET"
                             block:^(NSDictionary * result) {
                                 [self loadDataFinish:result];
    }];
}

-(void)loadDataFinish:(NSDictionary *)result{
    NSNumber * total_number = (NSNumber *)[result objectForKey:@"total_number"];
    NSLog(@"%@",total_number);
    NSArray * array = [result objectForKey:@"comments"];
    NSMutableArray * comments = [NSMutableArray arrayWithCapacity:array.count];
    for (NSDictionary * dic in array) {
        CommentModel * commentModel = [[[CommentModel alloc]initWithDataDic:dic]autorelease];
        [comments addObject:commentModel];
    }
    self.tableView.data = comments;
    [self.tableView reloadData];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 




- (void)dealloc {
    [_tableView release];
    [_userImageView release];
    [_nickLabel release];
    [_userBarView release];
    [super dealloc];
}
@end
